package com.huike.web.controller.review;


import com.huike.common.core.controller.BaseController;
import com.huike.common.core.domain.AjaxResult;
import com.huike.common.core.page.TableDataInfo;
import com.huike.common.utils.DateUtils;
import com.huike.common.utils.SecurityUtils;
import com.huike.review.doman.Review;
import com.huike.review.service.ReviewService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * 该Controller主要是为了复习三层架构以及Mybatis使用的，该部分接口已经放开权限，可以直接访问
 * 同学们在此处编写接口通过浏览器访问看是否能完成最简单的增删改查
 */
@RestController
@RequestMapping("/review")
public class MybatisReviewController extends BaseController {

    @Autowired
    private ReviewService reviewService;

    /**=========================================================新增数据============================================*/
    //请求路径新增
    @GetMapping("/saveData/{name}/{age}/{sex}")
    public AjaxResult saveDate(@PathVariable String name,@PathVariable Integer age,@PathVariable String sex){
        Review review = new Review();
        review.setName(name);
        review.setAge(age);
        review.setSex(sex);
        Date nowDate = DateUtils.getNowDate();
        review.setUpdateTime(nowDate);
/*        String username = SecurityUtils.getUsername();
        if (username != null){
            review.setCreateBy(username);
            return toAjax(reviewService.insertReview(review));
        }*/
        return toAjax(reviewService.insertReview(review));
    }

    //JSON新增
    @PostMapping("/saveData")
    public AjaxResult savePost(@RequestBody Review review){
        Date nowDate = DateUtils.getNowDate();
        review.setUpdateTime(nowDate);
/*        String username = SecurityUtils.getUsername();
        if (username != null){
        review.setCreateBy(username);
            return toAjax(reviewService.insertReview(review));
        }*/
        return toAjax(reviewService.insertReview(review));
    }
    /**=========================================================删除数据=============================================*/

    /**=========================================================修改数据=============================================*/

    @DeleteMapping("/remove/{id}")
    public AjaxResult remove(@PathVariable Long id){

        reviewService.deleteByid(id);

        return AjaxResult.success("删除成功");
    }

    //修改数据
    @PostMapping("/update")
    public AjaxResult update(@RequestBody Review review){
        reviewService.update(review);



        return AjaxResult.success( "修改成功");
    }
    /**=========================================================查询数据=============================================*/
    //单条件查询
    @GetMapping("/getById")
    public AjaxResult getById(Long id){
        Review review = reviewService.selectById(id);
        return AjaxResult.success(review);
    }

    //分页查询
    @GetMapping("/getDataByPage")
    public TableDataInfo selectList(Review review, HttpServletRequest req){
        startPage();
        List<Review> list = reviewService.selectList(review);
        return getDataTable(list);
    }


}